package exception01;

public class ExceptionDemo06 {
    public static void main(String[] args) {
        int[] arr = new int[3];
        try {
            int i = method(null, 2);
            System.out.println(i);
            int j = method(arr,5);
            System.out.println(j);
        }catch(NullPointerException e){
            System.out.println("捕获空指针异常");
        }catch(ArrayIndexOutOfBoundsException e ){
            System.out.println("索引超过数组索引范围");
        }
        System.out.println("程序执行结束");

    }
    public static int method(int[] arr,int index){
        if(arr==null){
            throw new NullPointerException("arr的值不能为null");
        }
        if(!(index>0 && index<=arr.length-1)){
            throw new ArrayIndexOutOfBoundsException("索引超过数组索引范围");
        }
        return arr[index];
    }
}
